package cn.dnui_cjy529.dnuifood_16110100529.controller;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.speech.RecognitionListener;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import cn.dnui_cjy529.dnuifood_16110100529.R;
import cn.dnui_cjy529.dnuifood_16110100529.bean.ResultBean;
import cn.dnui_cjy529.dnuifood_16110100529.listener.OnRetrofitListener;
import cn.dnui_cjy529.dnuifood_16110100529.model.UserModel;
import cn.dnui_cjy529.dnuifood_16110100529.MainActivity;


public abstract class RegisterActivity extends BaseActivity implements OnRetrofitListener<ResultBean>
{
    private Intent intent;
    private ImageView reg_return;
    private Button register;
    private EditText et_reg_user,et_reg_password,et_reg_repassword,
            et_reg_telephone,et_reg_adress,et_reg_comment;
    private UserModel userModel;

//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_register);
//        initViews();
//        initEvents();

//      Bundle bundle = new Bundle();       //携带成功注册的用户名参数
//        bundle.putString("data","test");
//        Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
//        intent.putExtras(bundle);
//        startActivity(intent);
    //}
    //@SuppressLint("CutPasteId")
    //@Override
    void initViews() {
        setLayout(R.layout.activity_register);
        register = (Button)findViewById(R.id.register);
        reg_return =(ImageView) findViewById(R.id.reg_return);
        et_reg_adress =(EditText) findViewById(R.id.et_reg_adress);
        et_reg_comment = (EditText) findViewById(R.id.et_reg_comment);
        et_reg_password = (EditText) findViewById(R.id.et_reg_password);
        et_reg_telephone = (EditText) findViewById(R.id.et_reg_telephone);
        et_reg_repassword = (EditText) findViewById(R.id.et_reg_repassword);
        et_reg_user =(EditText) findViewById(R.id.et_erg_user);
    }

    @Override
    void initEvents() {
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et_reg_password.getText().toString().equals(et_reg_repassword.getText().toString()))
                {
                    register();
                }
                else{
                    showToast("请确认两次密码相同");
                }
            }
        });


        reg_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent();
                intent.putExtra("user",et_reg_user.getText().toString());
                setResult(RESULT_OK,intent);
                finish();


            }
        });
    }

    private void register(){
        userModel=new UserModel();
        userModel.register(et_reg_user.getText().toString(),et_reg_password.getText().toString(),et_reg_telephone.getText().toString(),
                et_reg_adress.getText().toString(),et_reg_comment.getText().toString(),this);
    }

    @Override
    void initData() {

    }

    public void onsuccess(ResultBean resultBean, int flag) {
        if(resultBean.getSuccess().equals("1")){
            intent = new Intent();
            intent.putExtra("user",et_reg_user.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
            showToast("注册成功");
        }
        else {
            showToast("注册失败");
        }
    }



    @Override
    public void onFailure(String string) {
        showToast("网络发生错误");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_login_up);
    }



}

